package com.ty.blog.controller;

import com.ty.blog.common.utils.PageUtils;
import com.ty.blog.common.utils.R;
import com.ty.blog.entity.ContentEntity;
import com.ty.blog.service.blog.ContentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Map;

/**
 * 博客内容表
 *
 * @author zhutianyu
 * @email 1659770218@QQ.COM
 * @date 2021-07-28 14:29:38
 */
@Slf4j
@RestController
@RequestMapping("/content")
public class ContentController {
  @Resource
  private ContentService contentService;

  /**
   * 列表
   */
  @GetMapping("/list")
  public R list(@RequestParam Map<String, Object> params) {
    PageUtils page = contentService.queryPage(params);
    return R.ok().put("page", page);
  }

  /**
   * 信息
   */
  @GetMapping("/info/{id}")
  public R info(@PathVariable("id") Long id) {
    ContentEntity xBlogContent = contentService.getById(id);
    return R.ok().put("info", xBlogContent);
  }

  /**
   * 保存
   */
  @PostMapping("/save")
  public R save(@RequestBody ContentEntity xBlogContent) {
    return contentService.save(xBlogContent) ? R.ok() : R.error();
  }

  /**
   * 修改
   */
  @PatchMapping("/update")
  public R update(@RequestBody ContentEntity xBlogContent) {
    return contentService.updateById(xBlogContent) ? R.ok() : R.error();
  }

  /**
   * 删除
   */
  @DeleteMapping("/delete")
  public R delete(@RequestBody Long[] ids) {
    return contentService.removeByIds(Arrays.asList(ids)) ? R.ok() : R.error();
  }
}
